package org.ogv.collision;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Pool;

public class BoundingSphere {
	public float 	x, y, radius;
	public byte 	group;
	private static 	Sprite _debugTex;
	
	//Object management
	private static Pool<BoundingSphere> _POOL = new Pool<BoundingSphere>() {
		@Override
		protected BoundingSphere newObject() {
			return new BoundingSphere();
		}
	};
	public static void clearPool() 				{ _POOL.clear(); }
	public static BoundingSphere obtain() 		{ 
		if(_debugTex == null) _debugTex = new Sprite(new Texture(Gdx.files.internal("data/particle/particle.png")));
		return _POOL.obtain(); 
	}
	public void free()							{ _POOL.free(this); } 
	
	protected BoundingSphere() {}
	
	public BoundingSphere init(float x, float y, float radius, byte group) {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.group = group;
		
		return this;
	}
	
	public boolean isTouch(BoundingSphere other) {
		float dx = other.x - this.x;
		float dy = other.y - this.y;
		float sr = this.radius + other.radius;
		return dx*dx + dy*dy < sr*sr;
	}
	
	public boolean isTouch(float x, float y) {
		float dx = x - this.x;
		float dy = y - this.y;
		float sr = this.radius;
		return dx*dx + dy*dy < sr*sr;
	}
	
	public void debugDraw(SpriteBatch sb) {
		debugDraw(sb, 0, 0);
	}
	
	public void debugDraw(SpriteBatch sb, float offsetX, float offsetY) {
		_debugTex.setOrigin(0, 0);
		_debugTex.setScale(radius/(_debugTex.getTexture().getWidth() >> 1));
		_debugTex.setPosition(x - radius + offsetX, y - radius + offsetY);
		_debugTex.draw(sb);
	}
}
